;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "Iperformance.lisp")
(require "M-avl-string-keys.lisp")
(require "Mhelpers.lisp")
(require "Mstock-retrieval.lisp")

(module Mperformance
  (import Itree)
  (import Ihelpers)
  (import Istockretrieval)
  (include-book "list-utilities" :dir :teachpacks)
  (include-book "io-utilities" :dir :teachpacks)

; returns a pair of the ticker name and its performance
(defun performance (ticker start end tree)
  (let* ((stock1 (get-first-in-range tree start end ticker))
         (stock2 (get-last-in-range tree start end ticker))
         (closing-start (car (cddddr stock1)))
         (closing-end (car (cddddr stock2))))
    (list ticker (/ (+ closing-end closing-start) closing-start))))

; returns a list of pairs that all contain the name
; of a ticker and its performance in the range
(defun performance-query-with-tickers (tickers start end tree)
  (if (consp tickers)
      (cons (performance (car tickers) start end tree) (performance-query-with-tickers (cdr tickers) start end tree))
      nil))

; returns the maximum of all the performances returned
; using get-max
(defun performance-query (start end tickers tree)
  (if (consp tickers)
      (let* ((performances (performance-query-with-tickers tickers start end tree))
             (best (get-max performances (car performances))))
        (concatenate 'string (car best) ": " (rat->str (* 100 (cadr best)) 1) "%"))
      (let* ((performances (performance-query-with-tickers (get-all-tickers (get-stocks-in-range tree start end)) start end tree))
             (best (get-max performances (car performances))))
        (concatenate 'string (car best) ": " (rat->str (* 100 (cadr best)) 1) "%"))))
  
  (export Iperformance))